unit selgasto;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBCtrls, Grids, DBGrids, ExtCtrls, DB, StdCtrls, Buttons;

type
  TfrmSelGasto = class(TForm)
    Panel3: TPanel;
    grdGasto: TDBGrid;
    sGasto: TDataSource;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Panel2: TPanel;
    nvgGasto: TDBNavigator;
    btnGGE: TSpeedButton;
    btnGGO: TSpeedButton;
    procedure grdGastoDblClick(Sender: TObject);
    procedure nvgGastoClick(Sender: TObject; Button: TNavigateBtn);
    procedure FormCreate(Sender: TObject);
    procedure grdGastoDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure btnGGEClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    function RecuperarIdGasto: string;
    procedure EstablecerIdGasto (idGasto: string);
  public
    property idGasto: string read RecuperarIdGasto write EstablecerIdGasto;
    function Entrar: integer;
  end;

var
  frmSelGasto: TfrmSelGasto;

implementation

uses MDatos, main, Recursos;

{$R *.dfm}

function TfrmSelGasto.RecuperarIdGasto: string;
begin
  Result := Datos.cGastoID_GASTO.AsString;
end;

procedure TfrmSelGasto.EstablecerIdGasto (idGasto: string);
begin
  Datos.cGasto.Locate('id_gasto', idGasto, []);
end;

function TfrmSelGasto.Entrar: integer;
begin
  btnGGE.Enabled := (frmMain.nivel = 1);
  btnGGO.Enabled := btnGGE.Enabled;
  Result := Showmodal;
end;

procedure TfrmSelGasto.grdGastoDblClick(Sender: TObject);
begin
  ModalResult := mrOK;
end;

procedure TfrmSelGasto.nvgGastoClick(Sender: TObject;
  Button: TNavigateBtn);
begin
  if (Button in [nbPost, nbDelete]) then
    if Datos.cGasto.ChangeCount > 0 then
    begin
      Datos.cGasto.ApplyUpdates(0);
      Datos.cGasto.Refresh;
    end;
end;

procedure TfrmSelGasto.FormCreate(Sender: TObject);
begin
  Acomodo(frmSelGasto);
end;

procedure TfrmSelGasto.grdGastoDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
const
  clPaleGreen = TColor($CCFFCC);
  clPaleRed =   TColor($CCCCFF);
begin
  EjecutarSQL(Datos.qryVar, 'SELECT * FROM vincularid');
  if not(Datos.qryVar.IsEmpty) then
  begin
    if (Column.Field.Dataset.FieldbyName('id_gasto').AsInteger
    = Datos.qryVar.FieldByName('GGE').AsInteger) then
      if (gdFocused in State) then grdGasto.canvas.brush.color := clBlack
      else grdGasto.canvas.brush.color := clPaleRed;
    if (Column.Field.Dataset.FieldbyName('id_gasto').AsInteger
    = Datos.qryVar.FieldByName('GGO').AsInteger) then
      if (gdFocused in State) then grdGasto.canvas.brush.color := clBlack
      else grdGasto.canvas.brush.color := clPaleGreen;
    grdGasto.DefaultDrawColumnCell(rect,DataCol,Column,State);
  end;
end;

procedure TfrmSelGasto.btnGGEClick(Sender: TObject);
var
  campo: string;
begin
  if TButton(Sender).Name = 'btnGGE' then  campo := 'GGE'
  else campo := 'GGO';
  setearId(Datos.cGastoID_GASTO.AsString, campo);
  grdGasto.Repaint;
end;

procedure TfrmSelGasto.FormShow(Sender: TObject);
begin
  EjecutarSQL(Datos.qryVar, 'SELECT * FROM vincularid');
  if (Datos.qryVar.IsEmpty) then
    ShowMessage('Es necesario que defina un Costo Indirecto como '
    +'Gasto General de Empresa y otro como Gasto General de Obra')
  else if ((Datos.qryVar.FieldByName ('GGE').IsNull)
  or (Datos.qryVar.FieldByName ('GGO').IsNull)) then
    ShowMessage('Es necesario que defina un Costo Indirecto como '
    +'Gasto General de Empresa y otro como Gasto General de Obra');
end;

end.
